package com.jxc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.jxc.dao.TbRoleDAO;
import com.jxc.entity.TbMenu;
import com.jxc.entity.TbRole;
import com.jxc.dao.permissionrole;
import com.jxc.dao.TbMeunDAO;

@Transactional
public class permissionroleImpl implements permissionrole{

	@Resource private TbMeunDAO tbMenuDAO;
	@Resource private SessionFactory sessionFactory;
	//菜单列表
	public List<TbMenu> QueryMeunList() {
		
		return this.tbMenuDAO.findAll();
	}
	public  List QueryMeunListByParentID(int ID)
	{
		 return this.tbMenuDAO.findByParentID(ID);
	}
	//获取角色
	public List<Object[]> QueryRoleList() {
		
		String queryString="select roleId,name from TbRole";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
	//根据模块，获取权值
	public List QueryOpByMenu(String menuName) {
		
		String queryString="select opId,opName from TbPermission where tbMenu.name='"+menuName+"'";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
	//获取一级菜单
	
	public List QueryMenu(int orderby) {
		
		String queryString="from TbMenu where parentId=0 and orderby="+orderby;
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
 //获取二级菜单
	
	public List QueryMenu2(int orderby,int ParentOrderbyid) {
		
		String queryString="from TbMenu where orderby="+orderby+" and parentId="+ParentOrderbyid;
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
	//添加一级菜单
	public Boolean saveMenu(TbMenu menu) {
		
		try{
			sessionFactory.getCurrentSession().save(menu);
		}catch(RuntimeException re){
			
			return false;
		}
		return true;
	}
	//根据菜单名字，查询其id
	public List QueryMenuIDBymenuName(String menuName) {
		
		String queryString="select menuId from TbMenu where menuName='"+menuName+"'";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
	//根据菜单名字查id
	public List QueryMenuByName(String menuName) {
		
		String queryString="from TbMenu where menuName='"+menuName+"'";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
	//更新菜单
	public void updateMenu(TbMenu menu){
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(menu);
			
		} catch (RuntimeException re) {
			re.printStackTrace();
		}
	}
	//获取菜单总数
	public int QueryMenuCount() {
		
		String queryString="select count(menuId) from TbMenu where parentId=0";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		int count = Integer.valueOf(queryObject.iterate().next().toString());
		return count;
	}
	//获取菜单下，子菜单的个数
	public int QueryMenuIDandSubCount(int ParentId) {
		
		String queryString="select count(*) from TbMenu where parentId="+ParentId+"";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		int count = Integer.valueOf(queryObject.iterate().next().toString());
		return count;
	}
	//获取一级菜单2
	
	public List QueryMenuBig(int orderby) {
		
		String queryString="from TbMenu where parentId=0 and orderby>"+orderby;
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
	//根据菜单orderby删除菜单
	public void deleteMenuByOrderby(int orderby) {
		String sql="delete from tb_menu where parent_id=0 and orderby>"+orderby;
		sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
	}
	//根据父菜单ID，删除父菜单参数的值的子菜单
	public void deleteMenuSub(int ParentId) {
		String sql="delete from tb_menu where parent_id="+ParentId;
		sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
	}
	//根据菜单ID删除旗下小于orderby的子菜单
	public void deleteMenuSubByOrderby(int ParentId,int orderby) {
		String sql="delete from tb_menu where parent_id="+ParentId+" and orderby>"+orderby;
		sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
	}
	public List getRoleMenu(int roleId){
		
		String queryString="select tbPermission.tbMenu.menuId from TbPermissionRole where tbRole.roleId="+roleId+" and tbPermission.tbMenu.menuName=tbPermission.opName";
		Query queryObject = sessionFactory.getCurrentSession().createQuery(
				queryString);
		return queryObject.list();
	}
}
